package ajaxtest;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

public class huodongAction {
	
	public void delete_choujiang() {
		String sqlString = "delete from lucky_dog";
		DBManager dbManager = new DBManager();
		try {
			if (dbManager.deleteSQL(sqlString)) {
				String s = "{\"name\":\"提示\",\"age\":\"已删除全部抽奖数据，请重新抽奖！！！\"}";
				ServletActionContext.getResponse()
						.setCharacterEncoding("UTF-8");
				ServletActionContext.getResponse().getWriter().print(s);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			dbManager.closeConnection();
		}
	}

	public void delete_fenzu() {
		String sqlString = "delete from huodong";
		DBManager dbManager = new DBManager();
		try {
			if (dbManager.deleteSQL(sqlString)) {
				String s = "{\"name\":\"提示\",\"age\":\"已删除全部分组数据，请重新分组！！！\"}";
				ServletActionContext.getResponse()
						.setCharacterEncoding("UTF-8");
				ServletActionContext.getResponse().getWriter().print(s);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			dbManager.closeConnection();
		}

	}

	public void fenzu_view() {
		int huodong_id = Integer.valueOf(ServletActionContext.getRequest()
				.getParameter("huodong_id"));
		System.out.println(huodong_id);
		String sql = "select * from huodong where huodong_id=" + huodong_id;
		DBManager dbManager = new DBManager();
		ResultSet resultSet = dbManager.executeQuery(sql);
		List<huodong> huodongList = new ArrayList<>();
		huodong huodong = null;
		try {
			while (resultSet.next()) {
				huodong = new huodong();
				huodong.setGroup_id(resultSet.getInt("group_id"));
				huodong.setHuodong_id(resultSet.getInt("huodong_id"));
				huodong.setId(resultSet.getInt("id"));
				huodong.setNum(resultSet.getInt("num"));
				huodongList.add(huodong);
			}
			JSONArray jsonArray = JSONArray.fromObject(huodongList);
			System.out.println(jsonArray);
			// String s = "{\"name\":\"提示\",\"age\":\"已提交抢答申请，请静候抢答结果！！！\"}";
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().getWriter().print(jsonArray);

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			dbManager.closeConnection();
		}

	}

	public void fenzu() {
		DBManager dbManager = new DBManager();
		String sqlString = "";
		sqlString = "select * from huodong";
		ResultSet resultSet = dbManager.executeQuery(sqlString);
		try {
			if (resultSet.next()) {
				System.out.println("分组方案已经确定，不允许修改！！！");
				String s = "{\"name\":\"提示\",\"age\":\"分组方案已经确定，不允许修改！！！\"}";

				ServletActionContext.getResponse()
						.setCharacterEncoding("UTF-8");
				ServletActionContext.getResponse().getWriter().print(s);
				return;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

		int maxnumbers = Integer.valueOf(ServletActionContext.getRequest()
				.getParameter("maxnumbers"));
		System.out.println(maxnumbers);
		int[] a = new int[maxnumbers];
		for (int i = 1; i <= maxnumbers; i++)
			a[i - 1] = i;
		int result[] = new vder().getRandomFromArray(a, 36);
		for (int i = 0; i < result.length; i++) {
			System.out.println(result[i]);
		}

		int huodong_id = 0;
		int group_id = 0;

		for (int i = 1; i <= result.length; i++) {
			if (i <= 12) {// 第一次活动
				huodong_id = 1;
				if (i <= 3) {
					group_id = 1;
				} else if (i <= 6) {
					group_id = 2;
				} else if (i <= 9) {
					group_id = 3;
				} else {
					group_id = 4;
				}
			} else if (i <= 24) {// 第二次活动
				huodong_id = 2;
				if (i <= 15) {
					group_id = 1;
				} else if (i <= 18) {
					group_id = 2;
				} else if (i <= 21) {
					group_id = 3;
				} else {
					group_id = 4;
				}
			} else if (i <= 36) {// 第三次活动
				huodong_id = 3;
				if (i <= 27) {
					group_id = 1;
				} else if (i <= 30) {
					group_id = 2;
				} else if (i <= 33) {
					group_id = 3;
				} else {
					group_id = 4;
				}
			}
			sqlString = "insert into huodong (huodong_id,group_id,num)values("
					+ huodong_id + "," + group_id + "," + result[i - 1] + ")";
			System.out.println(sqlString);
			dbManager.insertSQL(sqlString);
		}

		try {
			String s = "{\"name\":\"提示\",\"age\":\"分组方案已生成！！！\"}";

			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().getWriter().print(s);
		} catch (Exception e) {
			// TODO: handle exception
		}

		dbManager.closeConnection();

	}
}
